library(stringr)

# Get the current page number from the file name
current_page <- as.numeric(str_extract(knitr::current_input(), "\\d+"))

# Set the total number of pages
total_pages <- 9

# Generate the URLs for the previous and next pages
previous_page <- ifelse(current_page > 1, paste0("visual_", current_page - 1, "-darfur_violence-code_included.html"), NA)
next_page <- ifelse(current_page < total_pages, paste0("visual_", current_page + 1, "-darfur_violence-code_included.html"), NA)


# Note: All code, insights shared, and methodological notes in this document and other documents in this portfolio ARE NOT open source and MUST be cited.

library(tidyverse)
library(ggplot2)
library(ggthemes)
library(plotly)

setwd("C:/Users/rsb84/Desktop/RB/COLUMBIA/QMSS/COURSES/Spring_2021/Data Visualization/End_project")

ACLED_data <- readxl::read_excel("ACLED-DARFUR-VAC-2008-2021 (After Course Ended-for 2023 Portfolio)-UPDATED VERSION-inter1_numbers_replaced_with_actor_names.xlsx", 
     col_types = c("date", "numeric", "text", 
         "text", "text", "text", "text", "text", 
         "text", "text", "text", "text", "text", "text", 
         "numeric", "numeric", "text", "text", 
         "numeric", "numeric"))

fatalities_by_year_region=ACLED_data %>% group_by(year, admin1) %>% tally(fatalities)


df=fatalities_by_year_region %>% group_by(year) %>% mutate(pct= prop.table(n) * 100)


d <- fatalities_by_year_region %>%
  rename(Year = year, Fatalities = n) %>%
  group_by(admin1) %>%
  mutate(Twenty_Fifth_Percentile = quantile(Fatalities, probs = 0.25),
         Seventy_Fifth_Percentile = quantile(Fatalities, probs = 0.75))

mins <- d %>%
  group_by(admin1) %>%
  slice(which.min(Fatalities))

maxs <- d %>%
  group_by(admin1) %>%
  slice(which.max(Fatalities))

ends <- d %>%
  group_by(admin1) %>%
  filter(Year == max(Year))


spark = ggplot(d, aes(x=Year, y=Fatalities)) +
  facet_grid(admin1 ~ ., scales = "free_y") + # panel.spacing.y adjusts the vertical space between sparklines in the facet
  geom_ribbon(aes(ymin = Twenty_Fifth_Percentile, max = Seventy_Fifth_Percentile), fill = 'grey80') +
  geom_line(size=0.75) +
  geom_point(data = mins, col = 'steelblue', size=8, nudge_y= -0.5, vjust = -0.5) +
  geom_point(data = maxs, col = 'red', size=8, nudge_y= 0.5, vjust = 0.5) +
  geom_text(data = mins, aes(label = Fatalities), nudge_y= -0.5, vjust = -0.5, color = "white", size = 4) +
  geom_text(data = maxs, aes(label = Fatalities), nudge_y= 0.5, vjust = 0.5, color = "white", size = 4) +
  geom_text(data = ends, aes(label = Fatalities), hjust = 0, nudge_x = 0.7) +
  geom_text(data = ends, aes(label = admin1), hjust = 0, nudge_x = 2.2, size=4, fontface="bold", color="black") +
  expand_limits(x = max(d$Year) + (0.25 * (max(d$Year) - min(d$Year)))) +
  scale_x_continuous(breaks = seq(2008, 2021, 2)) +
  scale_y_continuous(expand = c(0.1, 0)) +
  theme_tufte(base_size = 15, base_family = "Helvetica") +
  theme(title = element_text(size=15, color= "steelblue", face='bold'),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        axis.text.x = element_text(size = 11), axis.text.y = element_blank(),
        axis.ticks = element_blank(), strip.text = element_blank(),
        legend.position="none",
        panel.spacing.y = unit(2, "lines") # This line sets the vertical spacing between sparklines
        ) +
  labs(title="Civilian Killings by Region in Darfur, Sudan (2008 - 2021)",
       subtitle="", caption = "Source: The ACLED Dataset") +
  theme(plot.margin = unit(c(1,1,1,1), "lines"), text = element_text(face = "bold")) # Add margins around the plot, adjust the numbers to suit the layout

#theme(text = element_text(face = "bold"))
ggplotly(spark)


Takeaways: Investigating fatality numbers by region in Darfur reveals noteworthy trends. While true that by 2017 the number of civilian killings within each of Darfur’s five regions had subsided since their respective maximums, the number of killings in Central Darfur was higher than its interquartile range, adding questions as to why the UN Security Council was still determined to begin withdrawing the mission that year.


Additionally, by 2018, killings in West, South, and Central Darfur were near or above their respective seventy-fifth percentile values of their interquartile ranges. Yet, the UN continued to withdraw bases. A noticeable uptick in killings in West Darfur occurred in 2019, above the region’s interquartile range.


By the end of 2021, once UNAMID troops had all departed, there were already more civilian killings in West Darfur (155) than at any point since the mission had begun in 2008. With violence levels so high in several regions during the mission’s drawdown phase, one might have expected the UN to withdraw its remaining bases at least more slowly to prevent power vaccumes and spikes in violence.

# Get the current page number from the file name
current_page <- as.numeric(str_extract(knitr::current_input(), "\\d+"))

# Set the total number of pages
total_pages <- 9

# Generate the URLs for the previous and next pages
previous_page <- ifelse(current_page > 1, paste0("visual_", current_page - 1, "-darfur_violence-code_included.html"), NA)
next_page <- ifelse(current_page < total_pages, paste0("visual_", current_page + 1, "-darfur_violence-code_included.html"), NA)